using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using JlPay.Sdk.Core;
using JlPay.Sdk.Services.Ledger.BalanceSplit;
using JlPay.Sdk.Models.Ledger.BalanceSplit;

namespace JlPay.Sdk.Examples.Ledger.BalanceSplit.BalanceSplit
{
    /// <summary>
    /// 余额分账独立示例程序
    /// </summary>
    public class BalanceSplitDemo
    {
        /// <summary>
        /// 主程序入口
        /// </summary>
        /// <param name="args">命令行参数</param>
        /// <returns>任务</returns>
        public static async Task Main(string[] args)
        {
            Console.WriteLine("==========================================");
            Console.WriteLine("===== 余额分账独立示例程序 =====");
            Console.WriteLine("==========================================");
            Console.WriteLine();

            await BalanceSplitDemoAsync();

            Console.WriteLine("==========================================");
            Console.WriteLine("===== 示例运行完毕 =====");
            Console.WriteLine("==========================================");
        }

        /// <summary>
        /// 创建配置对象
        /// </summary>
        /// <returns>配置对象</returns>
        private static Config CreateConfig()
        {
            return DemoConfig.CreateConfigWithCrypto();
        }

        /// <summary>
        /// 构建余额分账请求
        /// </summary>
        /// <returns>请求对象</returns>
        private static BalanceSplitRequest BuildBalanceSplitRequest()
        {
            var request = new BalanceSplitRequest();
            
            // 设置请求参数
            request.OrgCode = "50720711";
            request.MchId = "849581183510017";
            request.OutOrderNo = "045e1314f7784f46bf9d590dc65c7";
            request.TotalAmount = "2000";
            request.Receivers = new List<Receiver>
            {
                new Receiver
                {
                    RevMchId = "849584307420004",
                    Amount = "500",
                    Description = "分账账户1"
                },
                new Receiver
                {
                    RevMchId = "849602058120001",
                    Amount = "1500",
                    Description = "分账账户2"
                }
            };
            
            return request;
        }

        /// <summary>
        /// 余额分账示例
        /// </summary>
        public static async Task BalanceSplitDemoAsync()
        {
            Console.WriteLine("开始执行余额分账示例");
            
            try
            {
                // 创建配置对象和余额分账服务
                var config = CreateConfig();
                var balanceSplitService = new BalanceSplitService(config);
                
                // 创建余额分账请求
                var request = BuildBalanceSplitRequest();
                
                // 发送请求
                var response = await balanceSplitService.SplitBalanceAsync(request);
                
                // 处理响应结果
                response.HandleResponse("余额分账", config);
            }
            catch (Exception ex)
            {
                var config = CreateConfig();
                var response = new BalanceSplitResponse();
                response.HandleResponseWithException("余额分账", config, ex);
            }
            
            Console.WriteLine("余额分账示例执行完毕");
        }
    }
} 